<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        const rate = 0.9;
        function request() {
            return new Promise((resolve, reject) => {
                setTimeout(() => {
                    let res = Math.random();
                    if (res > rate) {
                        resolve(res);
                    }
                    else {
                        reject(res)
                    }
                });
            })
        }
        function retry(func, time = 0, delay = 0) {
            return new Promise((resolve, reject) => {
                let inner = async function inner() {
                    try {
                        const result = await func();
                        resolve(result);
                    } catch (e) {
                        if (time-- <= 0) {
                            reject(e);
                        }
                        else {
                            console.log("开始重试剩余", time);
                            setTimeout(() => {
                                inner()
                            }, delay);
                        }
                    }

                }
                inner();
            })

        }
        retry(request, 3, 2000)
            .then(res => {
                console.log(res, "成功");
            })
            .catch(e => {
                console.log(e, "失败");
            })
    </script>
</body>

</html>